home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -in_the_mag- / workbench / adf / transdisk4.2 / transdisk.c < prev    next >
C/C++ Source or Header  |  2000-03-05  |  4KB  |  167 lines

  1. /*
  2.  * Transdisk V4.2
  3.  * Copyright 1995-97 Bernd Schmidt, Marcus Sundberg, Stefan Ropke,
  4.  *                   Rodney Hester, Joanne Dow
  5.  *
  6.  * Use DICE and 2.0 includes or above to compile. SAS C should work too.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <strings.h>
  13.  
  14. #include <exec/devices.h>
  15. #include <exec/io.h>
  16. #include <exec/memory.h>
  17. #include <devices/trackdisk.h>
  18.  
  19. #include <clib/alib_protos.h>
  20. #include <clib/exec_protos.h>
  21.  
  22. static void usage(void);
  23.  
  24. static void usage(void)
  25. {
  26.     fprintf(stderr, "Usage: transdisk options\n");
  27.     fprintf(stderr, "Recognized options:\n");
  28.     fprintf(stderr, "-h: Assume device is high density\n");
  29.     fprintf(stderr, "-d device unit: Use this device instead of DF0:\n");
  30.     fprintf(stderr, "-s n: Begin transfer at track n\n");
  31.     fprintf(stderr, "-e n: End transfer at track n\n");
  32.     fprintf(stderr, "-w filename: writes the ADF file <filename> to disk\n\n");
  33.     fprintf(stderr, "Example:\n");
  34.     fprintf(stderr, "transdisk >RAM:df1.adf.1 -d trackdisk 1 -s 0 -e 39\n");
  35.     fprintf(stderr, "transfers the first half of the floppy in DF1: into\n");
  36.     fprintf(stderr, "a file in the RAM disk.\n");
  37.     fprintf(stderr, "Or:\n");
  38.     fprintf(stderr, "transdisk -w test.adf\n");
  39.     fprintf(stderr, "writes the ADF-file test.adf to the disk in df0:\n");
  40. }
  41.  
  42. int main(int argc, char **argv)
  43. {
  44.     char *filename, *openMode="rb";
  45.     FILE *ADFFile;
  46.     int write=0;
  47.     struct IOStdReq *ioreq;
  48.     struct MsgPort *port;
  49.  
  50.     UBYTE *buffer;
  51.     char devicebuf[256];
  52.     char *devicename = "trackdisk.device";
  53.     char devicenum = 0;
  54.     int i;
  55.     int starttr = 0, endtr = 79;
  56.     int sectors = 11;
  57.  
  58.     for (i = 1; i < argc;) {
  59.     if (argv[i][0] != '-' || argv[i][2] != 0) {
  60.         usage();
  61.         exit(1);
  62.     }
  63.     switch (argv[i][1]) {
  64.      case 'h':
  65.         sectors = 22;
  66.         i++;
  67.         break;
  68.     case 'd':
  69.         if (i+2 >= argc) {
  70.         usage();
  71.         exit(1);
  72.         }
  73.         devicenum = atoi(argv[i+2]);
  74.         sprintf(devicebuf, "%s.device", argv[i+1]);
  75.         devicename = devicebuf;
  76.         i += 3;
  77.         break;
  78.     case 's':
  79.         if (i+1 >= argc) {
  80.         usage();
  81.         exit(1);
  82.         }
  83.         starttr = atoi(argv[i+1]);
  84.         i += 2;
  85.         break;
  86.     case 'e':
  87.         if (i+1 >= argc) {
  88.         usage();
  89.         exit(1);
  90.         }
  91.         endtr = atoi(argv[i+1]);
  92.         i += 2;
  93.         break;
  94.     case 'w':
  95.         if (i+1 >= argc) {
  96.         usage();
  97.         exit(1);
  98.         }
  99.         filename=argv[i+1];
  100.         write=1;
  101.         i += 2;
  102.         break;
  103.     default:
  104.         usage();
  105.         exit(1);
  106.     }
  107.     }
  108.     fprintf(stderr,"Using %s unit %d\n", devicename, devicenum);
  109.     fprintf(stderr,"Tracks are %d sectors\n", sectors);
  110.     fprintf(stderr,"First track %d, last track %d\n", starttr, endtr);
  111.  
  112.     buffer = AllocMem(512, MEMF_CHIP);
  113.     if (write) {
  114.     ADFFile = fopen(filename,openMode);
  115.  
  116.     if (!ADFFile) {
  117.         fprintf(stderr,"Error while opening input file\n");
  118.         exit (1);
  119.     }
  120.     }
  121.     
  122.     port = CreatePort(0, 0);
  123.     if (port) {
  124.     ioreq = CreateStdIO(port);
  125.     if (ioreq) {
  126.         if (OpenDevice(devicename, devicenum, (struct IORequest *) ioreq, 0) == 0) {
  127.         int tr, sec;
  128.  
  129.         ioreq->io_Command = write ? CMD_WRITE : CMD_READ;
  130.         ioreq->io_Length = 512;
  131.         ioreq->io_Data = buffer;
  132.         for (tr = starttr*2; tr < (endtr+1)*2; tr++) {
  133.             fprintf(stderr,"Track: %d\r",tr/2);
  134.             for (sec = 0; sec < sectors; sec++) {
  135.             fflush(stderr);
  136.             if (write)
  137.                 if (fread(buffer, sizeof(UBYTE), 512, ADFFile) < 512) {
  138.                 fprintf(stderr, "Error: ADF file to short?\n");
  139.                 exit(1);
  140.                 }  
  141.             ioreq->io_Offset = 512 * (tr * sectors + sec);
  142.             DoIO( (struct IORequest *) ioreq);
  143.             if (!write)
  144.                 fwrite(buffer, sizeof(UBYTE), 512, stdout);
  145.             }
  146.         }
  147.         if (write) {        /* Make sure the last track is written to disk */
  148.             ioreq->io_Command = CMD_UPDATE;
  149.             DoIO( (struct IORequest *) ioreq);
  150.         }
  151.         ioreq->io_Command = TD_MOTOR;    /* Turn Disk-motor off */
  152.         ioreq->io_Length = 0;
  153.         DoIO( (struct IORequest *) ioreq);
  154.         CloseDevice( (struct IORequest *) ioreq);
  155.         } else
  156.         fprintf(stderr,"Unable to open %s unit %d\n", devicename, devicenum);
  157.         DeleteStdIO(ioreq);
  158.     }
  159.     DeletePort(port);
  160.     }
  161.     fprintf(stderr,"\n");
  162.     FreeMem(buffer, 512);
  163.     if (write)
  164.     fclose (ADFFile);
  165.     return 0;
  166. }
  167.